from django.shortcuts import render
from home import models, serializers
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.cache import cache
from django.shortcuts import render
# Create your views here.
class TopNav(APIView):
    def get(self, request):
        top = models.Nav.objects.filter(isdel=False, postion=1).order_by('sort')
        topser = serializers.NavSer(top, many=True)
        return Response({"msg":"ok","data":topser.data,"code":200})
class FootNav(APIView):
    def get(self, request):
        foot = models.Nav.objects.filter(isdel=False, postion=0).order_by('sort')
        footser = serializers.NavSer(foot, many=True)
        return Response({"msg":"ok","data":footser.data,"code":200})
class Banner(APIView):
    def get(self,request):
        #读缓存中的数据  cach.get("key")  设置混存的数据  cache.set(key,val)
        #排序  有效数据
        cachedata=cache.get("banner")#去缓存中获取数据
        if cachedata:
            print(cachedata)
            #缓存中有数据 直接拿出来返回
            return Response({"msg":"ok","data":cachedata,"code":200})
        else:
            #缓存中没有数据  去数据库中查询数据  写到缓存中一份  然后返回给前端
            banner=models.Banner.objects.filter(isdel=False).order_by("sort")
            bannerser=serializers.Banner(banner,many=True)
            #将数据写到缓存中一份
            cache.set("banner",bannerser.data,60*60*24)#失效时间
            return Response({"msg":"ok","data":bannerser.data,"code":200})

from django.core.files.storage import default_storage
class uploads(APIView):
    def post(self,request):
        file = request.data.get('file')
        allow=["image/jpeg","image/png","image/jpg"]
        if file.content_type not in allow:
            return Response({"msg":"文件格式不支持"})
        if file.size > 1024*1024*2:
            return Response({"msg":"文件过大"})
        result = default_storage.save("user/"+file.name,file)
        url = default_storage.url(result)
        return Response({"msg":"上传成功","url":url})
class HomeCate(APIView):
    def get(self,request):
        #展示所有的一级分类的数据  条件 parent=None iddel=False
        allcate=models.Category.objects.filter(parent=None,isdel=False).order_by("sort")
        #序列化
        cateser=serializers.CategorySer(allcate,many=True)
        data=cateser.data
        for i in data:
            i["children"]=i["children"][:3]
        return Response({"code":200,"msg":"ok","data":data})
class ChildCate(APIView):
    def get(self,request):
        id=request.query_params.get("id","")
        if not id:
            return Response({"code":400,"msg":"id不能为空"})
        child=models.Category.objects.filter(id=id,isdel=False).first()
        cateser=serializers.CategorySer(child)
        return Response({"code":200,"msg":"ok","cate":cateser.data,"course":[]})

# class Bannerr(APIView):
#     def get(self,request):
#         huanxun= cache.get("banner")
#         if huanxun:
#             print(huanxun)
#             return Response({"code":200,"msg":"ok","data":huanxun})
#         else:
#             banner=models.Banner.objects.filter(isdel=False).order_by("sort")
#             bannerser=serializers.Banner(banner,many=True)
#             cache.set("banner",bannerser.data,60*60*24)
#             return Response({"code":200,"msg":"ok","data":bannerser.data})
